package main

import (
	"fmt"
	//"net"
	//"time"
	"runtime"
//	"math/rand"
	"sync"
)
var total_tickets int =500
var worker int =500
var quit =make(chan int,500)
var m *sync.Mutex
var input string
func sell_tk(i int){

//	for{
	m.Lock()
	if total_tickets>0{
//		time.Sleep(time.Duration(rand.Intn(5))*time.Millisecond)
//		runtime.Gosched()
	//	m.Lock()
		total_tickets--
	//	m.Unlock()
		fmt.Println("id:",i," ticket:",total_tickets)
		quit<-0
	 }else{
	 //close(quit)
	// break
	// fmt.Println("Close here")
	 }
	 m.Unlock()
//	}
}
func main(){
runtime.GOMAXPROCS(2)
m = new(sync.Mutex)
//rand.Seed(time.Now().Unix())

	for i:=0;i<worker;i++{
	go sell_tk(i)
	}

	fmt.Scanln(&input) // Mainthread wait here for  goroutine complete
	<-quit
//	fmt.Println(len(quit))
}

